package com.bstek;

import com.bstek.demo.BaseType;
import com.bstek.demo.ListType;
import com.bstek.demo.Obj4ListType;
import com.bstek.demo.PackageType;
import com.bstek.urule.Utils;
import com.bstek.urule.runtime.KnowledgePackage;
import com.bstek.urule.runtime.KnowledgeSession;
import com.bstek.urule.runtime.KnowledgeSessionFactory;
import com.bstek.urule.runtime.response.RuleExecutionResponse;
import com.bstek.urule.runtime.service.KnowledgeService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
public class SarielRuleClientApplication {
	public static void main(String[] args) throws IOException {
		ApplicationContext ctx = SpringApplication.run(SarielRuleClientApplication.class, args);
//        fireType();

	}

	private static void fireType(){
            //创建一个KnowledgeSession对象
            KnowledgeService knowledgeService = (KnowledgeService) Utils.getApplicationContext().getBean(KnowledgeService.BEAN_ID);
            KnowledgePackage knowledgePackage = null;
            try {
                    knowledgePackage = knowledgeService.getKnowledge("demo/demo_package");
            } catch (IOException e) {

            }
            KnowledgeSession session = KnowledgeSessionFactory.newKnowledgeSession(knowledgePackage);

            BaseType baseType = new BaseType();
            baseType.setBooleanValue(true);
            baseType.setCharValue('a');
            baseType.setDoubleValue(1.11);
            baseType.setFloatValue(1.1f);
            baseType.setIntValue(1);
            baseType.setLongValue(1000);
            baseType.setStringValue("string");
            session.insert(baseType);

            PackageType packageType = new PackageType();
            packageType.setBooleanValue(true);
            packageType.setCharValue('a');
            packageType.setDoubleValue(1.11);
            packageType.setFloatValue(1.1f);
            packageType.setIntValue(1);
            packageType.setLongValue(1000L);
            session.insert(packageType);

            ListType listType = new ListType();
            List<String> baseStrList= new ArrayList<>();
            baseStrList.add("a");
            listType.setBaseStrList(baseStrList);

            List<Obj4ListType> obj4ListTypeList= new ArrayList<>();
            Obj4ListType obj4ListType = new Obj4ListType();
            obj4ListType.setName("a");
            obj4ListTypeList.add(obj4ListType);
            listType.setObj4ListTypeList(obj4ListTypeList);

            List<ListType.InnerObj4ListType> innerObj4ListTypeList= new ArrayList<>();
            ListType.InnerObj4ListType innerObj4ListType = new ListType.InnerObj4ListType();
            innerObj4ListType.setName("abc");
            innerObj4ListTypeList.add(innerObj4ListType);
            listType.setInnerObj4ListTypeList(innerObj4ListTypeList);

            session.insert(listType);

            RuleExecutionResponse result = session.fireRules();
    }
}
